<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Exception;
use Log;

/**
 * 系统设置
 * Class Setting
 *
 * @package App\Models
 * @property int $id
 * @property int $tid
 * @property int $sort
 * @property int $award_id
 * @property string $index
 * @property string $value
 * @property string $created_at
 * @property string $updated_at
 * @mixin \Eloquent
 */
class PublicitySetting extends Model
{
    protected $table = 'xyj_publicity_setting';

    /**
     * 保存单个配置值
     * @param $index
     * @param $award_id
     * @param $value
     * @throws Exception
     */
    public static function saveOne( $award_id , $index , $value)
    {
        $model = self::where(['index'=> $index , 'award_id' => $award_id])->first();
        if (!$model) {
            $model = new PublicitySetting();
            $model->index = $index;
            $model->award_id = $award_id;
        }

        $model->value = $value;
        if (!$model->save()) {
            Log::info($model->getErrors());
            throw new Exception('保存失败。');
        }
    }

    /**
     * 检查配置值是否存在
     * @param $award_id
     * @param $index
     * @return bool
     */
    public static function checkExistOne($award_id , $index)
    {
        $model = self::where(['index'=> $index , 'award_id' => $award_id])->first();
        if ($model){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 调取单个配置值
     * @param $award_id
     * @param $index
     * @return $value
     */
    public static function getOne($award_id , $index)
    {
        $setting = self::where(['index'=> $index , 'award_id' => $award_id])->first();
        if ($setting){
            return $setting->value;
        }else{
            return null;
        }
    }

    /**
     * 保存单个作品类别
     * @param $award_id
     * @param $index
     * @param $value
     * @param $tid
     */
    public static function saveOneWorksType($award_id , $index , $value , $tid)
    {
        $model = new PublicitySetting();
        $model->index = $index;
        $model->award_id = $award_id;
        $model->tid = $tid;
        $model->value = $value;
        if (!$model->save()) {
            Log::info($model->getErrors());
            throw new Exception('保存失败。');
        }
    }

    /**
     * 保存单个命题
     * @param $award_id
     * @param $index
     * @param $value
     * @param $sort
     * @return $model
     */
    public static function saveOneCompany($award_id , $index , $value , $sort)
    {
        $model = new PublicitySetting();
        $model->index = $index;
        $model->award_id = $award_id;
        $model->sort = $sort;
        $model->value = $value;
        if (!$model->save()) {
            Log::info($model->getErrors());
            throw new Exception('保存失败。');
        }
        return $model;
    }

    /**
     * 保存单个奖项
     * @param $award_id
     * @param $index
     * @param $value
     */
    public static function saveOneVoteAward($award_id , $index , $value)
    {
        $model = new PublicitySetting();
        $model->index = $index;
        $model->award_id = $award_id;
        $model->value = $value;
        if (!$model->save()) {
            Log::info($model->getErrors());
            throw new Exception('保存失败。');
        }
    }

    /**
     * 调取命题名称
     */
    public function getCompanyName()
    {
        if ($this->index == 'company'){
            return PropositionCompany::find($this->value)->name;
        }else{
            return '';
        }
    }

    /**
     * 调取类别名称
     */
    public function getWorksTypeName()
    {
        if ($this->index == 'worksType'){
            return WorksType::find($this->value)->name;
        }else{
            return '';
        }
    }


    /**
     * 调取奖项名称
     * @return string
     */
    public function getVoteAwardName()
    {
        if ($this->index == 'voteAward'){
            return VoteAward::find($this->value)->name;
        }else{
            return '';
        }
    }

}
